package com.cyh.testcustomui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class LetterSideBar extends View {
    //定义26个字母
    public static String[] mLatters = {"A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y",
            "Z", "#"};
    private String mCrrentTouchLetter = ""; //当前触摸的字母
    private Paint mPaint;
    private int mTextsize = 20;
    private int mNormalColor = Color.BLUE;
    private int mFocusColor = Color.RED;
    public LetterSideBar(Context context) {
        this(context, null);
    }

    public LetterSideBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LetterSideBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LetterSideBar);
        mNormalColor = array.getColor(R.styleable.LetterSideBar_lsb_normal_color, mNormalColor);
        mFocusColor = array.getColor(R.styleable.LetterSideBar_lsb_focus_color, mFocusColor);
        mTextsize = array.getDimensionPixelSize(R.styleable.LetterSideBar_lsb_textsize, mTextsize);
        array.recycle();

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(mTextsize);
        mPaint.setColor(mNormalColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int textWidth = (int) mPaint.measureText("A");
        //计算宽度
        int width = getPaddingLeft() + getPaddingRight() + textWidth;

        //高度直接获取
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画26个字母
        int itemHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / mLatters.length;
        for(int i = 0; i < mLatters.length; i++) {
            //中心位置
            int letterCenterY = i * itemHeight + itemHeight / 2 + getPaddingTop();
            //基线

            Rect textBounds = new Rect();
            mPaint.getTextBounds(mLatters[i], 0, mLatters[i].length(), textBounds);
            Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
            int dy = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
            int baseLine = letterCenterY + dy; //中线 + dy

            int textWidth = (int)mPaint.measureText(mLatters[i]);
            int x = getWidth()/2 - textWidth/2;
            if(mCrrentTouchLetter.equals(mLatters[i])) {
                mPaint.setColor(mFocusColor); //最好用两个画笔，这样写性能差点，因为会调用native
            } else {
                mPaint.setColor(mNormalColor);
            }
            canvas.drawText(mLatters[i], x, baseLine, mPaint);

        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                float currentMoveY = event.getY();
                int itemHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / mLatters.length;
                int currentPosition = (int)((currentMoveY - getPaddingTop())/itemHeight);
                if(currentPosition >= 0 && currentPosition < mLatters.length) {
                    String currentTouchLetter = mLatters[currentPosition];
                    if(!currentTouchLetter.equals(mCrrentTouchLetter)) {
                        mCrrentTouchLetter = currentTouchLetter;
                        mLetterTouchListener.touch(mCrrentTouchLetter);
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                mLetterTouchListener.hide();
                break;
        }
        return true;
    }

    private LetterTouchListener mLetterTouchListener;

    public void setmCrrentTouchLetter(LetterTouchListener letter) {
        mLetterTouchListener = letter;
    }

    public interface LetterTouchListener{
        public void hide();
        public void touch(String letter);
    }
}
